package com.newasia.server.serviceImpl;

import com.newasia.server.dal.entitys.HisAlert;
import com.newasia.server.dal.entitys.paramValue.HisAlertParam;
import com.newasia.server.dal.persistence.HisAlertMapper;
import com.newasia.server.service.IHisAlertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author LiChong
 * @description 告警历史服务
 * @date 2020/5/19
 */

@Service
public class HisAlertServiceImpl implements IHisAlertService{

    @Autowired(required = false)
    HisAlertMapper hisAlertMapper;

    @Override
    public HisAlert selectHisAlert(String sid) {
        List<HisAlert> alerts = selectHisAlert(sid,1);
        if(alerts.size()>0){
            return alerts.get(0);
        }else{
            return null;
        }
    }

    @Override
    public HisAlert selectHisAlertSequence(String sid) {
        List<HisAlert> alerts = selectHisAlertSequence(sid,1);
        if(alerts.size()>0){
            return alerts.get(0);
        }else{
            return null;
        }
    }


    @Override
    public List<HisAlert> selectHisAlert(String sid, Integer number) {
        return selectHisAlert(sid,number,null);
    }

    @Override
    public HisAlert selectHisAlert(String sid, String fieldName) {
        List<HisAlert> alerts = selectHisAlert(sid,1, fieldName);
        if(alerts.size()>0){
            return alerts.get(0);
        }else{
            return null;
        }
    }

    @Override
    public HisAlert selectHisAlertFieldNotEmpty(String sid) {
        List<HisAlert> alerts = selectHisAlertFieldNotEmpty(sid,1);
        if(alerts.size()>0){
            return alerts.get(0);
        }else{
            return null;
        }
    }

    @Override
    public HisAlert selectHisAlert(String sid, Date startTime, Date endTime) {
        List<HisAlert> alerts = selectHisAlert(sid,1,null,startTime,endTime);
        if(alerts.size()>0){
            return alerts.get(0);
        }else{
            return null;
        }
    }

    @Override
    public List<HisAlert> selectHisAlert(String sid, Integer number, String fieldName) {
        return selectHisAlert(sid,number,fieldName,null,null);
    }

    @Override
    public List<HisAlert> selectHisAlertFieldNotEmpty(String sid, Integer number) {
        return selectHisAlertFieldNotEmpty(sid,number,null,null);
    }

    @Override
    public List<HisAlert> selectHisAlert(String sid, Integer number, Date startTime, Date endTime) {
        return selectHisAlert(sid,number,null,startTime,endTime);
    }

    @Override
    public List<HisAlert> selectHisAlertSequence(String sid, Integer number) {
        return selectHisAlertSequence(sid,number,null);
    }

    @Override
    public HisAlert selectHisAlertSequence(String sid, String fieldName) {
        List<HisAlert> alerts = selectHisAlertSequence(sid,1,fieldName);
        if(alerts.size()>0){
            return alerts.get(0);
        }else{
            return null;
        }
    }

    @Override
    public HisAlert selectHisAlertFieldNotEmpty(String sid, Date startTime, Date endTime) {
        List<HisAlert> alerts = selectHisAlertFieldNotEmpty(sid,1,startTime,endTime);
        if(alerts.size()>0){
            return alerts.get(0);
        }else{
            return null;
        }
    }

    @Override
    public List<HisAlert> selectHisAlert(String sid, Integer number, String fieldName, Date startTime, Date endTime) {
        return selectHisAlert(sid,number,fieldName,false,false,startTime,endTime);
    }

    @Override
    public List<HisAlert> selectHisAlertSequence(String sid, Integer number, String fieldName) {
        return selectHisAlertSequence(sid,number,fieldName,null,null);
    }

    @Override
    public List<HisAlert> selectHisAlertFieldNotEmpty(String sid, Integer number, Date startTime, Date endTime) {
        return selectHisAlert(sid,number,null,true,false,startTime,endTime);
    }

    @Override
    public List<HisAlert> selectHisAlertFieldNotEmptySequence(String sid, Integer number) {
        return selectHisAlertFieldNotEmpty(sid,number,null,null);
    }

    @Override
    public List<HisAlert> selectHisAlertSequence(String sid, Integer number, Date startTime, Date endTime) {
        return selectHisAlertSequence(sid,number,null,startTime,endTime);
    }

    @Override
    public List<HisAlert> selectHisAlertFieldNotEmptySequence(String sid, Integer number, Date startTime, Date endTime) {
        return selectHisAlert(sid,number,null,true,true,startTime,endTime);
    }

    @Override
    public List<HisAlert> selectHisAlertSequence(String sid, Integer number, String fieldName, Date startTime, Date endTime) {
        return selectHisAlert(sid,number,fieldName,false,true,startTime,endTime);
    }

    public List<HisAlert> selectHisAlert(String sid, Integer number, String fieldName,Boolean isNotEmpty,Boolean isSequence, Date startTime, Date endTime) {
        HisAlertParam param = new HisAlertParam();
        param.setSid(sid);
        param.setNumber(number);
        param.setFieldName(fieldName);
        param.setIsNotEmpty(isNotEmpty);
        param.setIsSequence(isSequence);
        param.setStartTime(startTime);
        param.setEndTime(endTime);
        return hisAlertMapper.selectHisAlert(param);
    }
}
